package lib_model.WareHouse;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Stocks_warehouse implements ISql {

	public int products_id_ref;
	public int warehouse_id_ref;
	public int quantity;
	public Date date_create;
	public Date date_update;

	public Stocks_warehouse() {
	}

	@Override
	public ReturnObj Select() {
		String sql = "select products_id_ref, warehouse_id_ref, quantity, date_create, date_update from dbo.stocks_warehouse";
		ReturnObj obj = new ReturnObj();
		ArrayList<Stocks_warehouse> arr = new ArrayList<Stocks_warehouse>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Stocks_warehouse a = new Stocks_warehouse();
				a.products_id_ref = rs.getInt("products_id_ref");
				a.warehouse_id_ref = rs.getInt("warehouse_id_ref");
				a.quantity = rs.getInt("quantity");
				a.date_create = rs.getDate("date_create");
				a.date_update = rs.getDate("date_update");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.stocks_warehouse (products_id_ref,   warehouse_id_ref,   quantity,   date_create,   date_update) values   (?,   ?,   ?,   GETDATE(),   GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.products_id_ref);
			preState.setInt(2, this.warehouse_id_ref);
			preState.setInt(3, this.quantity);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.stocks_warehouse set quantity = ?, date_create = ?, date_update = ? where products_id_ref = ? and warehouse_id_ref = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.warehouse_id_ref);
			preState.setInt(2, this.quantity);
			preState.setDate(3, (java.sql.Date) this.date_create);
			preState.setDate(4, (java.sql.Date) this.date_update);
			preState.setInt(5, this.products_id_ref);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.stocks_warehouse where products_id_ref = ? and warehouse_id_ref = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.products_id_ref);
			preState.setInt(2, this.warehouse_id_ref);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select products_id_ref, warehouse_id_ref, quantity, date_create, date_update from dbo.stocks_warehouse where products_id_ref = ? and warehouse_id_ref = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.products_id_ref);
			preState.setInt(2, this.warehouse_id_ref);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Stocks_warehouse a = new Stocks_warehouse();
				a.products_id_ref = rs.getInt("products_id_ref");
				a.warehouse_id_ref = rs.getInt("warehouse_id_ref");
				a.quantity = rs.getInt("quantity");
				a.date_create = rs.getDate("date_create");
				a.date_update = rs.getDate("date_update");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

}
